
import 'package:changxindianzishangwu/cache/local_cache.dart';
import 'package:changxindianzishangwu/dao/sms_code_dao.dart';
import 'package:changxindianzishangwu/dao/user_info_dao.dart';
import 'package:changxindianzishangwu/model/user_info_model.dart';
import 'package:changxindianzishangwu/model/base_model.dart';
import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/app_string.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/util/universal_view.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:changxindianzishangwu/widget/login_form_code.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

///绑定手机号=》填写验证码

class BindphoneCodePage extends StatefulWidget {
  String phone;
  String phoneCountry;
   BindphoneCodePage({Key key,this.phone,this.phoneCountry}) : super(key: key);

  @override
  _BindphoneCodePageState createState() => _BindphoneCodePageState(phone,phoneCountry);
}

class _BindphoneCodePageState extends State<BindphoneCodePage> {
  String _phone="";
  String _phoneCountry;


  _BindphoneCodePageState(this._phone, this._phoneCountry);

  static LoginFormCode _codeWidget ;

  static _getSmsCode(String p,String c) async {
    var result = await SmsCodeDao.getCode(p,
        c, FORGET_PASSWORD);
    EasyLoading.dismiss();
    if (result['resultCode'] == 0) {
      toast("获取验证码失败：${result["resultMsg"]}");
      return;
    }
    _codeWidget.startTime();
    toast("获取验证码成功");
  }

  TextEditingController _controller;

  @override
  void initState() {
    // TODO: implement initState
    _controller = TextEditingController();
    _codeWidget= LoginFormCode(
      available: true,
      onTapCallback: () {
        _getSmsCode(_phone,_phoneCountry);
      },
    );
    super.initState();

    EasyLoading.show( );
    _getSmsCode(_phone,_phoneCountry);
    print('-BindphoneCodePage--phoneCountry-----${_phoneCountry}');

  }

  @override
  void dispose() {
    _controller.dispose();
    EasyLoading.dismiss();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(context, "绑定手机号"),
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());

        },
        child: Container(
          color: Colors.white,
          padding: EdgeInsets.all(width(15)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "我们已向${TextUtil.hideNumber(_phone)}发送了验证码，请输入正确的验证码以进行下一步操作",
                style: _styleTv(15, AppColor.colorDefaultText),
              ),
              SizedBox(
                height: height(30),
              ),
              Text(
                "验证码",
                style: _styleTv(13, AppColor.colorText85),
              ),
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: TextField(
                      controller: _controller,
                      keyboardType: TextInputType.number,
                      cursorColor: Colors.greenAccent,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: "请输入验证码",
                        hintStyle: TextStyle(
                          fontSize: sp(13),
                          color: Color.fromRGBO(153, 153, 153, 1),
                        ),
                      ),
                    ),
                  ),
                  _codeWidget
                ],
              ),
              UniversalView().line(),
              SizedBox(
                height: height(60),
              ),
              UniversalView().longBtn("完成", () {
                if (_controller.text.isEmpty || _controller.text.length < 6) {
                  return toast("请输入验证码");
                }
                _bindPhone();

              })
            ],
          ),
        ),
      ),
    );
  }
  TextStyle _styleTv(double size, color) {
    return TextStyle(fontSize: sp(size), color: color);
  }

  void _bindPhone() async {
    BaseModel _baseModel=await UserInfoDao.userBindPhone(getUserInfo().imOpenId, _controller.text, _phone, _phoneCountry);
    if(_baseModel.resultCode!=1){
      toast("绑定失败:${_baseModel.resultMsg}");
      return;
    }
    UserInfo _userinfo=getUserInfo();
    _userinfo.phone=_phone;
    setUserinfoModel(_userinfo);
    Get.back(result: true);
  }
}
